package com.weixing.mall.provider.controller.rpc;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;
import com.weixing.mall.base.wrapper.Result;
import com.weixing.mall.base.wrapper.ResultUtil;
import com.weixing.mall.core.support.BaseController;
import com.weixing.mall.provider.api.model.dto.SearchItem;
import com.weixing.mall.provider.api.model.vo.GoodsDetailsVo;
import com.weixing.mall.provider.api.model.vo.GoodsPlaceVo;
import com.weixing.mall.provider.api.model.vo.GoodsPointDetailsVo;
import com.weixing.mall.provider.mapper.GoodsMapper;
import com.weixing.mall.provider.model.domain.Goods;
import com.weixing.mall.provider.model.domain.GoodsImg;
import com.weixing.mall.provider.model.domain.GoodsPoint;
import com.weixing.mall.provider.model.vo.GoodsPointVo;
import com.weixing.mall.provider.service.*;
import io.netty.util.ResourceLeakTracker;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

/**
 * @Author: kfc
 * @Description: <br/>
 * Date:Create in 2019/10/9 16:29
 * @Modified By:
 */
@RestController
@RequestMapping("/api/goods")
public class GoodsRpcController extends BaseController {

    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private IGoodsImgService goodsImgService;
    @Autowired
    private IGoodsVisitService goodsVisitService;
    @Autowired
    private IGoodsConcernService goodsConcernService;
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private IGoodsPrintService goodsPrintService;
    @Autowired
    private IGoodsPointService goodsPointService;
    /**
     * Description:  <br/>
     *
     * @param:
     * @return:
     * @Author: kfc
     * @Date: 2019/10/10 16:09
     */
    @GetMapping("selectGoodsSearchVo")
    public Result<SearchItem> selectGoodsSearchVo(long goodsId) {
        SearchItem itemVo = null;
        try {
            itemVo = goodsService.selectItemVo(goodsId);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return ResultUtil.success(itemVo);
    }
    @GetMapping("selectByGoodSn")
    public Result<List<GoodsPlaceVo>> selectGoodsSearchVo(List<String> goodsSnList ) {
        List<GoodsPlaceVo>  list= Lists.newArrayList();
        try {
          list= goodsService.listByGoodSn(goodsSnList);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultUtil.success(list);
    }
    @GetMapping("selectGoodsDetails")
    public Result<GoodsDetailsVo> selectGoodsDetails(Long goodsId){
        return ResultUtil.success(goodsService.selectById(goodsId));
    }

    @GetMapping("myGoodsHistory")
    public int selectMyGoodsHistory(Long memberId){
        return goodsVisitService.selectMyHistory(memberId);
    }
    @GetMapping("myGoodsStore")
    public int selectMyGoodsStore(Long memberId){
        return goodsConcernService.selectMyStore(memberId);
    }

    @GetMapping("byGoodsIdImg")
    public Result<String> byGoodsIdImg(Integer id){
        List<GoodsImg> list = goodsImgService.list(new QueryWrapper<GoodsImg>().eq("id", id.longValue()));
        return ResultUtil.success(list.get(0).getGoodsImg());
    }

    @GetMapping(value = "selectCommodityCode")
    public Result<String> selectCommodityCode(String goodsSn){
        String commodityCode = goodsPrintService.findCommodityCodeByGoodsSn(goodsSn);
        return ResultUtil.success().setData(commodityCode);
    }

    @GetMapping("selectGoodsPointDetails")
    public Result<GoodsPointDetailsVo> selectGoodsPointByGoodsPointId(Long goodsPointId) {
        GoodsPointDetailsVo itemVo = new GoodsPointDetailsVo();
        GoodsPoint goodsPoint = goodsPointService.getById(goodsPointId);
        if(ObjectUtil.isNotEmpty(goodsPoint)){
            BeanUtils.copyProperties(goodsPoint,itemVo);
        }
        return ResultUtil.success(itemVo);
    }

}
